package com.ahao.sdk;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.KeyEvent;

import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.List;

public class AHAOPluginManager implements IBasePlugin {
    private static AHAOPluginManager instance = new AHAOPluginManager();

    public static AHAOPluginManager getInstance() {
        return instance;
    }

    private List<IBasePlugin> plugins = new ArrayList<>();

    /**
     * 通过类反射实例化插件并注册
     *
     * @param className 插件类名
     */
    public void registerPlugin(String className) {
        try {
            Class<?> pluginClass = Class.forName(className);
            Constructor<?> constructor = pluginClass.getConstructor();
            IBasePlugin plugin = (IBasePlugin) constructor.newInstance();
            plugins.add(plugin);
        } catch (Exception e) {
            e.printStackTrace(); // 处理异常
        }
    }

    /**
     * 移除插件
     *
     * @param plugin 插件实例
     */
    public void unregisterPlugin(IBasePlugin plugin) {
        plugins.remove(plugin);
    }

    @Override
    public void onEmit(Context var1, String var2, String var3) {
        for (IBasePlugin plugin : plugins) {
            plugin.onEmit(var1, var2, var3);
        }
    }

    @Override
    public void initApplication(Application application) {
        for (IBasePlugin plugin : plugins) {
            plugin.initApplication(application);
        }
    }

    @Override
    public void onCreate(Activity activity) {
        for (IBasePlugin plugin : plugins) {
            plugin.onCreate(activity);
        }
    }

    @Override
    public void onStart() {
        for (IBasePlugin plugin : plugins) {
            plugin.onStart();
        }
    }

    @Override
    public void onPause() {
        for (IBasePlugin plugin : plugins) {
            plugin.onPause();
        }
    }

    @Override
    public void onResume() {
        for (IBasePlugin plugin : plugins) {
            plugin.onResume();
        }
    }

    @Override
    public void onNewIntent(Intent var1) {
        for (IBasePlugin plugin : plugins) {
            plugin.onNewIntent(var1);
        }
    }

    @Override
    public void onStop() {
        for (IBasePlugin plugin : plugins) {
            plugin.onStop();
        }
    }

    @Override
    public void onDestroy() {
        for (IBasePlugin plugin : plugins) {
            plugin.onDestroy();
        }
    }

    @Override
    public void onRestart() {
        for (IBasePlugin plugin : plugins) {
            plugin.onRestart();
        }
    }

    @Override
    public void onConfigurationChanged(Configuration var1) {
        for (IBasePlugin plugin : plugins) {
            plugin.onConfigurationChanged(var1);
        }
    }

    @Override
    public void onSaveInstanceState(Bundle var1) {
        for (IBasePlugin plugin : plugins) {
            plugin.onSaveInstanceState(var1);
        }
    }

    @Override
    public void onActivityResult(int var1, int var2, Intent var3) {
        for (IBasePlugin plugin : plugins) {
            plugin.onActivityResult(var1, var2, var3);
        }
    }

    @Override
    public void onBackPressed() {
        for (IBasePlugin plugin : plugins) {
            plugin.onBackPressed();
        }
    }

    @Override
    public boolean onKeyDown(int var1, KeyEvent var2) {
        for (IBasePlugin plugin : plugins) {
            if (plugin.onKeyDown(var1, var2)) {
                return true;
            }
        }
        return false;
    }

    @Override
    public void onRequestPermissionsResult(int var1, String[] var2, int[] var3) {
        for (IBasePlugin plugin : plugins) {
            plugin.onRequestPermissionsResult(var1, var2, var3);
        }
    }
}
